package constraints;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IsDateValidator implements ConstraintValidator<IsDate, String> {
	private String format;
	
	public void initialize(IsDate constraintAnnotation) {
		this.format = constraintAnnotation.format();
	}

	public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
		try {
			SimpleDateFormat sf = new SimpleDateFormat(format);
			sf.setLenient(false);
			sf.parse((String)object);
			
			return true;
		} catch (ParseException pe) {
			return false;
		}
	}

}
